package com.jt.service;

import com.jt.vo.ImageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
public class FileServiceImpl implements FileService{

//    String localDir = "E:/workspace/images";
    String localDir = "/usr/local/src/images";
    String preUrl = "http://image.jt.com";
    /**
     * 业务说明：
     *    1.检验图片的类型
     *
     * */

    @Override
    public ImageVO upload(MultipartFile file) {

        //1.校验图片的类型
        //1.1 获取图片名称
        String fileName = file.getOriginalFilename();
        //1.2 将字母转为小写
        fileName = fileName.toLowerCase();
        //1.3 正则表达式写法
        if (!fileName.matches("^.+\\.(jpg|png|gif)$")){
            return null;
        }
        //2.防止木马病毒  图片宽度和高度
        try {
            BufferedImage bufferedImage =
                    ImageIO.read(file.getInputStream());
            int width = bufferedImage.getWidth();
            int height = bufferedImage.getHeight();
            if (width == 0 || height == 0){
                //说明  不是正经图片
                return null;
            }
            //3.分目录存储 按提交的时间|名称hash|type  进行存户
            String datePath = new SimpleDateFormat("/yyyy/MM/dd/")
                    .format(new Date());
            //3.1创建图片目录
            String dirPath = localDir + datePath;
            //3.2 创建目录
            File dirFile = new File(dirPath);
            if (!dirFile.exists()){
                dirFile.mkdirs();
            }

            //4.要求文件名称不重复
            String uuid = UUID.randomUUID().toString();
            int index = fileName.lastIndexOf(".");
            String type = fileName.substring(index);
            fileName = uuid + type;

            //5.实现文件上传
            String realFilePath = dirPath + fileName;
            file.transferTo(new File(realFilePath));

            //6.返回ImageVO对象
            String virtualPath = datePath + fileName;
//            String urlPath = "https://img14.360buyimg.com/n0/jfs/t1/163644/7/19814/352369/607f3d80E2e8e47f7/93f9e0079a36f59f.jpg";
            String urlPath = preUrl + virtualPath;
            ImageVO imageVO = new ImageVO(virtualPath,urlPath,fileName);
            System.out.println(urlPath);
            return imageVO;

        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public void deleteFile(String virtualPath) {
        String path = localDir + virtualPath;
        File file = new File(path);
        if (file.exists()){
            file.delete();
        }
    }

    /*@Override
    public ImageVO upload(MultipartFile file) {

        //1.获取文件名称 abc.jpg
        String fileName = file.getOriginalFilename();

        //2.指定磁盘路径信息
        String filePath = "E:/workspace/images";

        //3.校验目录是否存在
        File dirFile = new File(filePath);
        if(!dirFile.exists()){
            dirFile.mkdirs(); //创建多级目录
        }

        //4.准备文件的全路径   F:/JT_SOFT/images/abc.jpg
        String path = filePath + "/" +fileName;

        //5.将路径包装为File对象
        File imageFile = new File(path);
        //6.实现文件上传
        try {
            file.transferTo(imageFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }*/





}
